<template>
  <div class="tc fs-4 p-2">软件管理 {{ seleceMode }}</div>

  <!-- 查询的表单 -->
  <NFlex justify="end" class="p-5">
    <NForm inline>
      <NFormItem>
        <NInput v-model:value="queryInfo.sname" placeholder="软件名称"></NInput>
      </NFormItem>
      <NFormItem>
        <NSpace>
          <NButton type="primary" @click="selectQuery">{{ btnText }}</NButton>
          <NButton type="error" @click="reset">重置</NButton>
          <NButton type="success" @click="requery">查询</NButton>
          <NButton type="default" @click="showAdd">添加</NButton>
        </NSpace>
      </NFormItem>
    </NForm>
  </NFlex>

  <!-- 数据表格 -->
  <div>
    <NDataTable :loading="loading" :data="list" :columns="columns"></NDataTable>
  </div>

  <!-- 游戏发行商选择对话框 -->
  <NModal preset="dialog" v-model:show="svisible" title="请选择软件的分类">
    <div>
      <SoftwareTypeComp mode="selector" @info-selected="selectPublisher"></SoftwareTypeComp>
    </div>
  </NModal>

  <!-- 分页信息 -->
  <div>
    <NFlex justify="center">
      <NPagination
        v-model:page="page.pageNumber"
        :page-size="page.pageSize"
        :item-count="page.total"
        @update-page="query"
      ></NPagination>
    </NFlex>
  </div>

  <!-- 添加软件的对话框 -->
  <NModal @after-leave="requery" preset="dialog" v-model:show="avisible" title="添加游戏">
    <div>
      <NForm>
        <NFormItem>
          <NButton @click="svisible = true">{{ btnAddText }}</NButton>
        </NFormItem>
        <NFormItem>
          <NInput v-model:value="addInfo.sname" placeholder="软件名称"></NInput>
        </NFormItem>
        <NFormItem>
          <NInput type="textarea" v-model:value="addInfo.intro" placeholder="软件简介"></NInput>
        </NFormItem>
        <NFormItem>
          <NInput type="textarea" v-model:value="addInfo.vendor" placeholder="软件厂商"></NInput>
        </NFormItem>
      </NForm>
    </div>
    <template #action>
      <NSpace>
        <NButton :loading="loading" type="primary" @click="add">添加</NButton>
        <NButton @click="avisible = false"> 关闭</NButton>
      </NSpace>
    </template>
  </NModal>

  <!-- 修改游戏的对话框 -->
  <NModal @after-leave="requery" preset="dialog" v-model:show="mvisible" title="修改软件">
    <div>
      <NForm>
        <NFormItem>
          <NButton @click="svisible = true">{{ btnModifyText }}</NButton>
        </NFormItem>
        <NFormItem>
          <NInput v-model:value="modifyInfo.sname" placeholder="软件名称"></NInput>
        </NFormItem>
        <NFormItem>
          <NInput type="textarea" v-model:value="modifyInfo.intro" placeholder="软件简介"></NInput>
        </NFormItem>
        <NFormItem>
          <NInput type="textarea" v-model:value="modifyInfo.vendor" placeholder="软件简介"></NInput>
        </NFormItem>
      </NForm>
    </div>
    <template #action>
      <NSpace>
        <NButton :loading="loading" type="primary" @click="modify">修改</NButton>
        <NButton @click="mvisible = false"> 关闭</NButton>
      </NSpace>
    </template>
  </NModal>
</template>

<script setup lang="ts">
import { ref, h, onMounted } from 'vue'
import {
  useMessage,
  NDataTable,
  NForm,
  NFormItem,
  NInput,
  NButton,
  NModal,
  NPagination,
  NFlex,
  NSpace,
  useDialog,
} from 'naive-ui'
import type { DataTableColumn } from 'naive-ui'
import type { BaseDataResult, BaseResult, PageBean, PageVo } from '@/types/BaseResult'
import type { TbSoftware, TbSoftwareType } from '@/types/Crud'
import { Api } from '@/types/Api'
import { Tools } from '@/types/Tools'
import SoftwareTypeComp from '@/components/SoftwareTypeComp.vue'
const message = useMessage()
const dialog = useDialog()

const loading = ref(false)

const selecteInfo = ref({} as TbSoftware)
const seleceMode = ref('query')

const selectMode = ref('add')
const svisible = ref(false)
const btnText = ref('请选择发行商')

//#region 选择发行商的部分
const selectPublisher = (info: TbSoftwareType) => {
  svisible.value = false
  if (selectMode.value == 'query') {
    btnText.value = info.tname
    queryInfo.value.stid = info.stid
  } else if (seleceMode.value == 'add') {
    btnAddText.value = info.tname
    addInfo.value.stid = info.stid
  }
}

//#endregion

//#region 查询

const page = ref({ pageNumber: 1, pageSize: 5 } as PageBean)
const queryInfo = ref({ stid: -1, sname: '' })
const list = ref([] as Array<TbSoftware>)

const selectQuery = () => {
  selectMode.value = 'query'
  btnText.value = '请选择软件的分类'
  queryInfo.value.stid = -1
  svisible.value = true
}
const columns = ref([
  {
    title: '软件名称',
    key: 'sname',
  },
  {
    title: '软件介绍',
    key: 'intro',
    render(row) {
      return h('div', { class: 'col-16' }, row.intro)
    },
  },
  {
    title: '软件厂商',
    key: 'vendor',
  },
  {
    title: '软件的分类',
    key: 'TbSoftwareType.stid',
  },
  {
    title: '操作',
    key: 'action',
    render(row) {
      return h(NSpace, {}, [
        h(
          NButton,
          {
            sizi: 'small',
            type: 'info',
            onClick: () => {
              showModify(row)
            },
          },
          '修改',
        ),
        h(
          NButton,
          {
            sizi: 'small',
            type: 'error',
            onClick: () => {
              showDelete(row)
            },
          },
          '删除',
        ),
      ])
    },
  },
] as DataTableColumn<TbSoftware>[])

const requery = () => {
  page.value.pageNumber = 1
  query()
}

const reset = () => {
  queryInfo.value = { stid: -1, sname: '' }
  page.value.pageNumber = 1
  btnText.value = '请选择软件的分类'
  query()
}
const query = () => {
  Api.get(
    `/crud/software/${page.value.pageSize}/${page.value.pageNumber}`,
    queryInfo.value,
    (data: BaseDataResult<PageVo<TbSoftware>>) => {
      loading.value = false
      if (data.success) {
        list.value = data.data.list
        page.value = data.data.pageBean
        return
      }
      message.error(data.message)
    },
  )
}

onMounted(() => {
  query()
})

//#endregion

//#region  添加的部分
const avisible = ref(false)
const addInfo = ref({} as TbSoftware)
const btnAddText = ref('请选择软件的分类')

const resetAdd = () => {
  addInfo.value.stid = -1
  addInfo.value.sname = ''
  addInfo.value.intro = ''
  addInfo.value.vendor = ''
  btnAddText.value = '请选择发行商'
}

const showAdd = () => {
  resetAdd()
  seleceMode.value = 'add'
  avisible.value = true
}

const add = () => {
  loading.value = true
  Api.post('/crud/software', addInfo.value, (data: BaseResult) => {
    loading.value = false
    if (data.success) {
      message.success('添加成功')
      avisible.value = false
      return
    }
    message.error(data.message)
  })
}

//#endregion

//#region 修改的部分
const mvisible = ref(false)
const modifyInfo = ref({} as TbSoftware)
const btnModifyText = ref('请选择发行商')

const showModify = (row: TbSoftware) => {
  modifyInfo.value = Tools.concatJson(row) as TbSoftware
  btnModifyText.value = row.type.tname
  seleceMode.value = 'modify'
  mvisible.value = true
}

const modify = () => {
  loading.value = true
  Api.put('/crud/software', modifyInfo.value, (data: BaseResult) => {
    loading.value = false
    if (data.success) {
      message.success('修改成功')
      mvisible.value = false
      return
    }
    message.error(data.message)
  })
}

//#endregion

//#region  删除的部分
const showDelete = (row: TbSoftware) => {
  dialog.warning({
    title: '警告',
    content: `确定要删除${row.sname}吗？`,
    positiveText: '确定',
    negativeText: '取消',
    onPositiveClick: () => {
      loading.value = true
      Api.delete(`/crud/software/${row.sid}`, {}, (data: BaseResult) => {
        loading.value = false
        if (data.success) {
          message.success('删除成功')
          requery()
          return
        }
        message.error(data.message)
      })
    },
  })
}

//#endregion
</script>

<style scoped lang="scss"></style>
